<template>
	<view>
    <view class="search-container">
      <view class="search-box">
        <ul class="item-list" v-if="type === 0">
          <li class="item-list-item">
            <view class="item-list-item-left">用户名</view>
            <view class="item-list-item-right">{{username}}</view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">项目名称</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入项目名称" v-model="queryProject.F_FullName">
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">项目编号</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入项目编号" v-model="queryProject.F_ProjectCode">
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">开始时间</view>
            <view class="item-list-item-right">
              <picker
                  class="picker-component"
                  :class="emptyProjectStartTime ? 'text-gray' : ''"
                  mode="date"
                  :value="queryProject.StartTime"
                  @change="bindProjectStartDateChange">
                <view class="uni-input">{{emptyProjectStartTime ? '请选择开始时间' : queryProject.StartTime}}</view>
              </picker>
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">结束时间</view>
            <view class="item-list-item-right">
              <picker
                  class="picker-component"
                  :class="emptyProjectEndTime ? 'text-gray' : ''"
                  mode="date"
                  :value="queryProject.EndTime"
                  @change="bindProjectEndDateChange">
                <view class="uni-input">{{emptyProjectEndTime ? '请选择结束时间' :queryProject.EndTime}}</view>
              </picker>
            </view>
          </li>
<!--          <li class="item-list-item">-->
<!--            <view class="item-list-item-left">项目进度</view>-->
<!--            <view class="item-list-item-right">-->
<!--              <picker-->
<!--                  class="picker-component"-->
<!--                  :value="selectedProjectIndex"-->
<!--                  :range="projectStatus"-->
<!--                  range-key="name"-->
<!--                  @change="bindProjectStatusChange" >-->
<!--                <view class="uni-input">{{selectedProjectStatusName}}</view>-->
<!--              </picker>-->
<!--            </view>-->
<!--          </li>-->
        </ul>
        <ul class="item-list" v-if="type === 1">
          <li class="item-list-item">
            <view class="item-list-item-left">用户名</view>
            <view class="item-list-item-right">{{username}}</view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">焊工姓名</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入焊工姓名" v-model="queryWelder.F_RealName">
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">焊工编号</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入焊工编号" v-model="queryWelder.F_WelderCode">
            </view>
          </li>
        </ul>
        <ul class="item-list" v-if="type === 2">
          <li class="item-list-item">
            <view class="item-list-item-left">用户名</view>
            <view class="item-list-item-right">{{username}}</view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">开始时间</view>
            <view class="item-list-item-right">
              <picker
                  class="picker-component"
                  :class="emptyHotStartTime ? 'text-gray' : ''"
                  mode="date"
                  :value="queryHot.StartTime"
                  @change="bindHotStartDateChange">
                <view class="uni-input">{{emptyHotStartTime ? '请选择开始时间' : queryHot.StartTime}}</view>
              </picker>
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">结束时间</view>
            <view class="item-list-item-right">
              <picker
                  class="picker-component"
                  :class="emptyHotEndTime ? 'text-gray' : ''"
                  mode="date"
                  :value="queryHot.EndTime"
                  @change="bindHotEndDateChange">
                <view class="uni-input">{{emptyHotEndTime ? '请选择结束时间' :queryHot.EndTime}}</view>
              </picker>
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">项目编号</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入项目编号" v-model="queryHot.F_ProjectCode">
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">项目名称</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入项目名称" v-model="queryHot.F_ProjectName">
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">设备编号</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入设备编号" v-model="queryHot.F_MachineCode">
            </view>
          </li>
<!--          <li class="item-list-item">-->
<!--            <view class="item-list-item-left">焊工姓名</view>-->
<!--            <view class="item-list-item-right">-->
<!--              <input type="text" placeholder="请输入焊工姓名" v-model="queryHot.F_WelderName">-->
<!--            </view>-->
<!--          </li>-->
          <li class="item-list-item">
            <view class="item-list-item-left">焊接结果</view>
            <view class="item-list-item-right">
              <picker @change="bindHotResultChange" :value="selectedHotIndex" :range="weldResult" range-key="name">
                <view class="uni-input">{{selectedHotResultName}}</view>
              </picker>
            </view>
          </li>
        </ul>
        <ul class="item-list" v-if="type === 3">
          <li class="item-list-item">
            <view class="item-list-item-left">用户名</view>
            <view class="item-list-item-right">{{username}}</view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">开始时间</view>
            <view class="item-list-item-right">
              <picker
                  class="picker-component"
                  :class="emptyElectricStartTime ? 'text-gray' : ''"
                  mode="date"
                  :value="queryElectric.StartTime"
                  @change="bindElectricStartDateChange">
                <view class="uni-input">{{emptyElectricStartTime ? '请选择开始时间' : queryElectric.StartTime}}</view>
              </picker>
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">结束时间</view>
            <view class="item-list-item-right">
              <picker
                  class="picker-component"
                  :class="emptyElectricEndTime ? 'text-gray' : ''"
                  mode="date"
                  :value="queryElectric.EndTime"
                  @change="bindElectricEndDateChange">
                <view class="uni-input">{{emptyElectricEndTime ? '请选择结束时间' : queryElectric.EndTime}}</view>
              </picker>
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">项目编号</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入项目编号" v-model="queryElectric.F_ProjectCode">
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">项目名称</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入项目名称" v-model="queryElectric.F_ProjectName">
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">设备编号</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入设备编号" v-model="queryElectric.F_MachineCode">
            </view>
          </li>
<!--          <li class="item-list-item">-->
<!--            <view class="item-list-item-left">焊工姓名</view>-->
<!--            <view class="item-list-item-right">-->
<!--              <input type="text" placeholder="请输入焊工姓名" v-model="queryElectric.F_WelderName">-->
<!--            </view>-->
<!--          </li>-->
          <li class="item-list-item">
            <view class="item-list-item-left">焊接结果</view>
            <view class="item-list-item-right">
              <picker @change="bindElectricResultChange" :value="selectedElectricIndex" :range="weldResult" range-key="name">
                <view class="uni-input">{{selectedElectricResultName}}</view>
              </picker>
            </view>
          </li>
        </ul>
        <ul class="item-list" v-if="type === 4">
          <li class="item-list-item">
            <view class="item-list-item-left">设备编号</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入设备编号" v-model="queryDevice.F_MachineCode">
            </view>
          </li>
          <li class="item-list-item">
            <view class="item-list-item-left">公司id</view>
            <view class="item-list-item-right">
              <input type="text" placeholder="请输入公司id" v-model="queryDevice.F_CompanyId">
            </view>
          </li>
        </ul>
				<button type="primary" @click="search">查询</button>
			</view>
    </view>
    <message ref="message"></message>
	</view>
</template>

<script>

  import {getTodayDate, getTomorrow} from "../../js/common";

  export default {
		data() {
			return {
        username: uni.getStorageSync("username"),
        projectStatus: [
          {name: '全部', value: -1},
          {name: '立项', value: 1},
          {name: '开工', value: 2},
          {name: '完工', value: 3},
          {name: '停工', value: 4}
        ],
        type: null,
        queryProject: {
          F_FullName: '',
          F_ProjectCode: '',
          StartTime: '',
          EndTime: '',
          // F_ScheduleId: -1
        },
        // 选中了项目状态索引为几的选项
        selectedProjectIndex: 0,
        queryWelder: {
          F_RealName: '',
          F_WelderCode: ''
        },
        weldResult: [
          {name: '全部', value: -1},
          {name: '失败', value: 0},
          {name: '成功', value: 1},
          // {name: '失败（翻边阶段人为终止）', value: 2},
          // {name: '失败（渗热阶段人为终止）', value: 3},
          // {name: '失败（切换阶段人为终止）', value: 4},
          // {name: '失败（升压阶段人为终止）', value: 5},
          // {name: '失败（冷却阶段人为终止）', value: 6},
          // {name: '失败（翻边压力超差）', value: 7},
          // {name: '失败（冷却压力超差）', value: 8},
          // {name: '失败（翻边温度超差）', value: 9},
          // {name: '失败（吸热温度超差）', value: 10},
          // {name: '失败（切换时间超差）', value: 11},
          // {name: '失败（升压时间超差）', value: 12},
          // {name: '失败（设备断电）', value: 13},
          // {name: '失败（对接高压碰撞）', value: 14}
        ],
        queryHot: {
          StartTime: '',
          EndTime: '',
          F_ProjectCode: '',
          F_ProjectName: '',
          F_MachineCode: '',
          F_ResultCode: -1
        },
        // 选中了热熔记录状态索引为几的选项
        selectedHotIndex: 0,
        queryElectric: {
          StartTime: '',
          EndTime: '',
          F_ProjectCode: '',
          F_ProjectName: '',
          F_MachineCode: '',
          F_ResultCode: -1
        },
        // 选中了电熔记录状态索引为几的选项
        selectedElectricIndex: 0,
        queryDevice: {
          F_MachineCode: '',
          F_CompanyId: ''
        },
			}
		},
    computed: {
		  /**
       * 判断项目开始时间是否为空
       */
      emptyProjectStartTime() {
        return !this.queryProject.StartTime
      },
      /**
       * 判断项目结束时间是否为空
       */
      emptyProjectEndTime() {
        return !this.queryProject.EndTime
      },
      /**
       * 选择好的项目状态的名称
       */
      selectedProjectStatusName() {
        let selected = this.projectStatus.filter(item => item.value === this.queryProject.F_ScheduleId);
        if(selected.length === 1) {
          return selected[0].name
        } else {
          return '查无此状态'
        }
      },
      /**
       * 判断热熔记录开始时间是否为空
       */
      emptyHotStartTime() {
        return !this.queryHot.StartTime
      },
      /**
       * 判断热熔记录结束时间是否为空
       */
      emptyHotEndTime() {
        return !this.queryHot.EndTime
      },
      /**
       * 选择好的热熔记录结果的名称
       */
      selectedHotResultName() {
        let selected = this.weldResult.filter(item => item.value === this.queryHot.F_ResultCode);
        if(selected.length === 1) {
          return selected[0].name
        } else {
          return '查无此状态'
        }
      },
      /**
       * 判断电熔记录开始时间是否为空
       */
      emptyElectricStartTime() {
        return !this.queryElectric.StartTime
      },
      /**
       * 判断电熔记录结束时间是否为空
       */
      emptyElectricEndTime() {
        return !this.queryElectric.EndTime
      },
      /**
       * 选择好的电熔记录结果的名称
       */
      selectedElectricResultName() {
        let selected = this.weldResult.filter(item => item.value === this.queryElectric.F_ResultCode);
        if(selected.length === 1) {
          return selected[0].name
        } else {
          return '查无此状态'
        }
      }
    },
		methods: {
      /**
       * 项目状态picker的选择事件
       * @param e 事件对象
       */
      bindProjectStatusChange(e) {
        this.selectedProjectIndex = e.detail.value;
        this.queryProject.F_ScheduleId = this.projectStatus[this.selectedProjectIndex].value;
      },
      /**
       * 项目开始时间picker的选择事件
       */
      bindProjectStartDateChange(e) {
        this.queryProject.StartTime = e.detail.value;
      },
      /**
       * 项目结束时间picker的选择事件
       */
      bindProjectEndDateChange(e) {
        this.queryProject.EndTime = e.detail.value;
      },
      /**
       * 热熔记录状态picker的选择事件
       * @param e 事件对象
       */
      bindHotResultChange(e) {
        this.selectedHotIndex = e.detail.value;
        this.queryHot.F_ResultCode = this.weldResult[this.selectedHotIndex].value;
      },
      /**
       * 热熔记录开始时间picker的选择事件
       */
      bindHotStartDateChange(e) {
        this.queryHot.StartTime = e.detail.value;
      },
      /**
       * 热熔记录结束时间picker的选择事件
       */
      bindHotEndDateChange(e) {
        this.queryHot.EndTime = e.detail.value;
      },
      /**
       * 电熔记录状态picker的选择事件
       * @param e 事件对象
       */
      bindElectricResultChange(e) {
        this.selectedElectricIndex = e.detail.value;
        this.queryElectric.F_ResultCode = this.weldResult[this.selectedElectricIndex].value;
      },
      /**
       * 电熔记录开始时间picker的选择事件
       */
      bindElectricStartDateChange(e) {
        this.queryElectric.StartTime = e.detail.value;
      },
      /**
       * 电熔记录结束时间picker的选择事件
       */
      bindElectricEndDateChange(e) {
        this.queryElectric.EndTime = e.detail.value;
      },
      /**
       * 按照type的值进行不同的查找
       */
      search() {
        switch (this.type) {
          case 0:
            this.searchProject();
            break;
          case 1:
            this.searchWelder();
            break;
          case 2:
            this.searchHot();
            break;
          case 3:
            this.searchElectric();
            break;
          case 4:
            this.searchDevice();
            break;
        }
      },
      /**
       * 项目查询
       */
      searchProject() {
        let query = JSON.parse(JSON.stringify(this.queryProject));
        // -1是查询全部
        if (query.F_ScheduleId === -1) {
          query.F_ScheduleId = null;
        }
        if (!query.StartTime) {
          query.StartTime = '1970-01-01'
        }
        if (!query.EndTime) {
          query.EndTime = getTomorrow();
        }
        uni.navigateTo({
          url: '/pages/project/list?query=' + encodeURIComponent(JSON.stringify(query))
        })
      },
      /**
       * 焊工查询
       */
      searchWelder() {
        let query = JSON.parse(JSON.stringify(this.queryWelder));
        uni.navigateTo({
          url: '/pages/welder/list?query=' + encodeURIComponent(JSON.stringify(query))
        })
      },
      /**
       * 热熔记录查询
       */
      searchHot() {
        let query = JSON.parse(JSON.stringify(this.queryHot));
        // -1是查询全部
        if (query.F_ResultCode === -1) {
          query.F_ResultCode = null;
        }
        if (!query.StartTime) {
          query.StartTime = '1970-01-01'
        }
        if (!query.EndTime) {
          query.EndTime = getTomorrow();
        }
        uni.navigateTo({
          url: '/pages/hot/list?query=' + encodeURIComponent(JSON.stringify(query))
        })
      },
      /**
       * 电容记录查询
       */
      searchElectric() {
        let query = JSON.parse(JSON.stringify(this.queryElectric));
        // -1是查询全部
        if (query.F_ResultCode === -1) {
          query.F_ResultCode = null;
        }
        if (!query.StartTime) {
          query.StartTime = '1970-01-01'
        }
        if (!query.EndTime) {
          query.EndTime = getTomorrow();
        }
        uni.navigateTo({
          url: '/pages/electric/list?query=' + encodeURIComponent(JSON.stringify(query))
        })
      },
      /**
       * 设备查询
       */
      searchDevice() {
        let query = JSON.parse(JSON.stringify(this.queryDevice));
        uni.navigateTo({
          url: '/pages/device/list?query=' + encodeURIComponent(JSON.stringify(query))
        })
      },
      /**
       * 根据type设置标题
       */
      setTitleForType() {
        switch (this.type) {
          case 0:
            uni.setNavigationBarTitle({
              title: '项目查询'
            })
            break;
          case 1:
            uni.setNavigationBarTitle({
              title: '焊工查询'
            })
            break;
          case 2:
            uni.setNavigationBarTitle({
              title: '热熔查询'
            })
            break;
          case 3:
            uni.setNavigationBarTitle({
              title: '电熔查询'
            })
            break;
          case 4:
            uni.setNavigationBarTitle({
              title: '设备查询'
            })
            break;
        }
      }
		},
    onLoad(option) {
		  this.type = parseInt(option.type);
      this.setTitleForType();
    },
    onReady() {
      this.$refs.message.setMessageToStore(this.$refs.message)
    }
	}
</script>

<style scoped lang="scss">
	@import "~@/static/css/search.scss";	
</style>
